import { Component, Sprite, UITransform, _decorator } from "cc";
import { TILE_HIGHT, TILE_WIDTH } from "db://assets/Scripts/Tile/TileManager";
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAM_NAME_ENUM } from "db://assets/Enums";
import EventManager from "db://assets/Runtime/EventManager";
import { PlayerStateMachine } from "db://assets/Scripts/Player/PlayerStateMachine";
import { IEntity } from "../Levels";
import StateMachine from "./StateMachine";
import { randomByLen } from "../utils";

const { ccclass, property } = _decorator


@ccclass('EntityManager')
export class EntityManager extends Component {
  id: string = randomByLen(12)
  x: number = 0
  y: number = 0
  fsm: StateMachine
  private _direction: DIRECTION_ENUM = DIRECTION_ENUM.TOP
  private _state: ENTITY_STATE_ENUM
  private type: ENTITY_TYPE_ENUM

  get direction() {
    return this._direction
  }

  set direction(newDirection) {
    this._direction = newDirection
    this.fsm?.setParams(PARAM_NAME_ENUM.DIRECTION, DIRECTION_ORDER_ENUM[this._direction])
  }

  get state() {
    return this._state
  }

  set state(newState) {
    this._state = newState
    this.fsm.setParams(this._state, true)
  }

  async init(params: IEntity) {
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM

    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WIDTH * 4, TILE_HIGHT * 4)

    this.x = params.x
    this.y = params.y
    this.type = params.type
    this.direction = params.direction
    this.state = params.state
  }

  update() {
    this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HIGHT + TILE_HIGHT * 1.5)
    // console.log(this.node.getPosition(), this.x, this.y)
  }

  onDestroy(){}
}
